package cn.com.mall.product.service.impl;

import cn.com.mall.product.entity.SkuImagesEntity;
import cn.com.mall.product.entity.SpuInfoDescEntity;
import cn.com.mall.product.service.SkuImagesService;
import cn.com.mall.product.service.SpuInfoDescService;
import cn.com.mall.product.vo.SkuItemVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.mall.common.utils.PageUtils;
import cn.com.mall.common.utils.Query;

import cn.com.mall.product.dao.SkuInfoDao;
import cn.com.mall.product.entity.SkuInfoEntity;
import cn.com.mall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupServiceImpl attrGroupService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String catelogId_str=(String)params.get("catelogId");
        String brandId_str=(String)params.get("brandId");
        String min_str=(String)params.get("min");
        String max_str=(String)params.get("max");
        String key=(String)params.get("key");
        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();

        if(!"0".equals(catelogId_str)){
            Long catelogId=Long.parseLong(catelogId_str);
            skuInfoEntityQueryWrapper.eq("catalog_id",catelogId);
        }
        if(!"0".equals(brandId_str)){
            Long brandId=Long.parseLong(brandId_str);
            skuInfoEntityQueryWrapper.eq("brand_id",brandId);

        }
        if(!"0".equals(min_str)){
            BigDecimal min=new BigDecimal(min_str);
            skuInfoEntityQueryWrapper.ge("price",min);

        }
        if(!"0".equals(max_str)){
            BigDecimal max =new BigDecimal(max_str);

            skuInfoEntityQueryWrapper.le("price",max);

        }
        if(!StringUtils.isEmpty(key)){
            skuInfoEntityQueryWrapper.and(item->{
                item.eq("sku_id",key).or().like("sku_name",key).or().like("sku_desc",key);
            });
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),skuInfoEntityQueryWrapper

        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo=new SkuItemVo();

        //获取sku基本信息
        SkuInfoEntity skuInfoEntity = getById(skuId);
        skuItemVo.setSkuInfoEntity(skuInfoEntity);
        Long catalogId = skuInfoEntity.getCatalogId();

        //获取sku的图片信息
//        SkuImagesEntity skuImagesEntity = skuImagesService.getOne(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
        List<SkuImagesEntity> skuImagesEntityList= skuImagesService.getImagesBySkuId(skuId);

        skuItemVo.setImages(skuImagesEntityList);

        //当前spu的销售属性组合

        //spu的介绍
        Long spuId = skuInfoEntity.getSpuId();
        SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(spuId);
        skuItemVo.setDesc(spuInfoDescEntity);

        //获取spu的规格参数信息
        List<SkuItemVo.SpuItemAttrGroupVo> groupAttrs=attrGroupService.getAttrGroupsBySpuId(spuId, catalogId);

        skuItemVo.setGroupAttrs(groupAttrs);

        //

        return null;
    }

}